﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Utilty;
using RED_NET_Core.Entity;

namespace RED_NET.Manager_s
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["PageName"] = "Manager";
            DropDownList1.Visible = false;//超级管理员才能访问
            if (Session["USER"] == null)
            {
                Response.Write("<script>alert('请先登录！');location.href='Index.aspx';</script>");
            }
            if (!IsPostBack)//第一次进入页面
            {
                LoginBLL nlogin = new LoginBLL();
                Users user = (Users)Session["USER"];
                int userId = user.UserId;
                int right = nlogin.GetRight(userId);
                ViewState["User_Right"] = right;
                if (right == 3)//超级管理员
                {
                    DropDownList1.Visible = true;
                    ViewState["RightIndex"] = 0;
                    GetCollegeBind();
                    ViewState["CollegeIndex"] = 0;
                    GetMajorBind(Int32.Parse(collegeList.SelectedValue));
                    ViewState["MajorIndex"] = 0;
                    GetClassBind(Int32.Parse(majorList.SelectedValue));
                    ViewState["ClassIndex"] = 0;
                }
                else if(right == 2)//校管理员
                {
                    GetCollegeBind();
                    ViewState["CollegeIndex"] = 0;
                    GetMajorBind(Int32.Parse(collegeList.SelectedValue));
                    ViewState["MajorIdIndex"] = 0;
                    GetClassBind(Int32.Parse(majorList.SelectedValue));
                    ViewState["ClassIndex"] = 0;
                }
                else if (right == 1)
                {
                    Label6.Visible = collegeList.Visible = false;
                    int collegeId = nlogin.GetCollegeIdOfUser(userId);
                    ViewState["CollegeId"] = collegeId;
                    GetMajorBind(collegeId);
                    ViewState["MajorIdIndex"] = 0;
                    GetClassBind(Int32.Parse(majorList.SelectedValue));
                    ViewState["ClassIndex"] = 0;
                }
                else
                {
                    Response.Redirect("~/ContentPage/Error/PageNotFound.aspx");//*代表错误页面，表示无权访问该页面
                }
            }
            //int uid; = Int32.Parse(Session["USER_ID"].ToString());
            /*else
            {
               if (right == 3)//超级管理员
               {
                   /*
                   DropDownList1.Visible = true;
                   DropDownList1.SelectedIndex = Int32.Parse(ViewState["RightIndex"].ToString());
                   collegeList.SelectedIndex = Int32.Parse(ViewState["CollegeIndex"].ToString());
                   majorList.SelectedIndex = Int32.Parse(ViewState["MajorIndex"].ToString());
                   classList.SelectedIndex = Int32.Parse(ViewState["ClassIndex"].ToString());
                    * 
                   ViewState["CollegeIndex"] = collegeList.SelectedIndex;
                   GetMajorBind(Int32.Parse(collegeList.SelectedValue));
                   ViewState["MajorIndex"] = 0;
                   GetClassBind(Int32.Parse(majorList.SelectedValue));
                   ViewState["ClassIndex"] = 0;
               }
               else if (right == 2)
               {
                   collegeList.SelectedIndex = Int32.Parse(ViewState["CollegeIndex"].ToString());
                   majorList.SelectedIndex = Int32.Parse(ViewState["MajorIndex"].ToString());
                   classList.SelectedIndex = Int32.Parse(ViewState["ClassIndex"].ToString());
               }
               else if (right == 1)
               {
                   Label6.Visible = collegeList.Visible = false;
                   majorList.SelectedIndex = Int32.Parse(ViewState["MajorIndex"].ToString());
                   classList.SelectedIndex = Int32.Parse(ViewState["ClassIndex"].ToString());
               }
               else
               {
                 Response.Redirect("*");//*代表错误页面，表示无权访问该页面
               }
            }
             * */
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile == false)
            {
                Response.Write("<script>alert('请选择要导入的Excel表！')</script>");
                return;
            }
            string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
            if (IsXls != ".xls")
            {
                Response.Write("<script>alert('只可选择Excel文件！')</script>");
                return;
            }
            string ImageUrl = "~/Image_Css/Image/Photo_Images/";
            string filename = DateTime.Now.ToString("yyyymmddMMss") + FileUpload1.FileName;
            string savePath = Server.MapPath("../UpFiles/" + filename);
            FileUpload1.SaveAs(savePath);
            Manager_BLL manager = new Manager_BLL();
            try
            {
                int NotInsetedNum = manager.BatchInsetIntoUser(manager.GetDataSetByExcel(
                    savePath, DateTime.Now.ToString("yyyymmddMMss") + filename));
                if (NotInsetedNum < 0) Response.Write("<script>alert('Excel表格格式错误！');</script>");
                else
                {
                    Response.Write("<script>alert('有" + NotInsetedNum.ToString() + "记录因用户名已存在或数据错误而无法导入！');</script>");
                }
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    int USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
        }

        //绑定学院
        protected void GetCollegeBind()
        {
            Manager_BLL manager = new Manager_BLL();
            try
            {
                collegeList.DataSource = manager.GetColleges();
                collegeList.DataTextField = "_CollegeName";
                collegeList.DataValueField = "_CollegeId";
                collegeList.DataBind();
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    int USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
        }
        //绑定专业
        protected void GetMajorBind(int tCollegeId)
        {
            Manager_BLL manager = new Manager_BLL();
            try
            {
                majorList.DataSource = manager.GetMajorsByCollege(tCollegeId);
                majorList.DataTextField = "_MajorName";
                majorList.DataValueField = "_MajorId";
                majorList.DataBind();
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    int USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
        }
        //绑定班级
        protected void GetClassBind(int tMajorId)
        {
            Manager_BLL manager = new Manager_BLL();
            try
            {
                classList.DataSource = manager.GetClassesByMajor(tMajorId);
                classList.DataTextField = "_ClassName";
                classList.DataValueField = "_ClassId";
                classList.DataBind();
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    int USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
        }
        //学院选择改变触发专业和班级信息重新绑定
        protected void CollegeSelectedChanged(object sender, EventArgs e)
        {
            if(ViewState["User_Right"].ToString() == "3")//每次页面刷新 要记录不相联的数据并重新赋值
            {
                DropDownList1.Visible = true;
                DropDownList1.SelectedIndex = Int32.Parse(ViewState["RightIndex"].ToString());
            }
            ViewState["CollegeIndex"] = collegeList.SelectedIndex;
            GetMajorBind(Int32.Parse(collegeList.SelectedValue));
            ViewState["MajorIndex"] = 0;
            GetClassBind(Int32.Parse(majorList.SelectedValue));
            ViewState["ClassIndex"] = 0;
        }
        //专业选择改变触发班级信息重新绑定
        protected void MajorSelectedChanged(object sender, EventArgs e)
        {
            if (ViewState["User_Right"].ToString() == "3")
            {
                DropDownList1.Visible = true;
                collegeList.SelectedIndex = Int32.Parse(ViewState["CollegeIndex"].ToString());
                DropDownList1.SelectedIndex = Int32.Parse(ViewState["RightIndex"].ToString());
            }
            else if (ViewState["User_Right"].ToString() == "2")
            {
                collegeList.SelectedIndex = Int32.Parse(ViewState["CollegeIndex"].ToString());
            }
            else
            {
                Label6.Visible = collegeList.Visible = false;
            }
            ViewState["MajorIndex"] = majorList.SelectedIndex;
            GetClassBind(Int32.Parse(majorList.SelectedValue));
            ViewState["ClassIndex"] = 0;
        }
        //注册
        protected void Btn_Register_Click(object sender, EventArgs e)
        {
            String username = userName.Text.Trim();
            String password_one = firstPsw.Text.Trim();
            String password_two = secondPsw.Text.Trim();
            String nickname = nickName.Text.Trim();
            //检测用户名
            int k = Int32.Parse(Safety.IsValidStr(username,25).value.ToString());
            if (k == 8)
            {
                Response.Write("<script>alert('用户名不能为空！');</script>");
            }
            else if (k == 9)
            {
                Response.Write("<script>alert('用户名应少于25位！');</script>");
            }
            else if (k == 10)
            {
                Response.Write("<script>alert('用户名包含非法字符！');</script>");
            }
            else
            {
                if (username.Length < 6)
                {
                    Response.Write("<script>alert('用户名应大于6位！');</script>");
                }
            }

            if (password_one == password_two)
            {
                //检测密码
                k = Int32.Parse(Safety.IsValidStr(password_one, 25).value.ToString());
                if (k == 8)
                {
                    Response.Write("<script>alert('密码不能为空！');</script>");
                    return;
                }
                else if (k == 9)
                {
                    Response.Write("<script>alert('密码应少于25位！');</script>");
                    return;
                }
                else if (k == 10)
                {
                    Response.Write("<script>alert('密码包含非法字符！');</script>");
                    return;
                }
                else
                {
                    if (password_one.Length < 6)
                    {
                        Response.Write("<script>alert('密码应大于6位！');</script>");
                        return;
                    }
                    else
                    {
                        int college_id = Int32.Parse(collegeList.SelectedValue);
                        int major_id = Int32.Parse(majorList.SelectedValue);
                        int class_id = Int32.Parse(classList.SelectedValue);
                        Manager_BLL manager = new Manager_BLL();
                        int k2 = Int32.Parse(ViewState["RightIndex"].ToString());
                        //如果是注册团支书用户
                        try
                        {
                            manager.Registe(username, password_one, college_id, major_id, class_id, k2, nickname);
                        }
                        catch (Exception ex)
                        {
                            //将错误信息插入数据库
                            if (Session["USER"] != null)
                            {
                                Users user = (Users)Session["USER"];
                                int USER_ID = user.UserId;
                                DateTime time = DateTime.Now;
                                ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                            }
                            Response.Redirect("~/ContentPage/Error/PageError.aspx");
                        }
                        Response.Write("<script>alert('注册成功！');</script>");
                    }
                }
            }
            else
            {
                Response.Write("<script>alert('两次密码不相同！');</script>");
                return;
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Manager's/Manager_UpdateImage.aspx");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["RightIndex"] = DropDownList1.SelectedIndex;
            if (ViewState["User_Right"].ToString() == "3")
            {
                DropDownList1.Visible = true;
                collegeList.SelectedIndex = Int32.Parse(ViewState["CollegeIndex"].ToString());
                DropDownList1.SelectedIndex = Int32.Parse(ViewState["RightIndex"].ToString());
            }
            else if (ViewState["User_Right"].ToString() == "2")
            {
                collegeList.SelectedIndex = Int32.Parse(ViewState["CollegeIndex"].ToString());
            }
            else
            {
                Label6.Visible = collegeList.Visible = false;
            }
            majorList.SelectedIndex = Int32.Parse(ViewState["MajorIndex"].ToString());
            classList.SelectedIndex = Int32.Parse(ViewState["ClassIndex"].ToString());
            int k = Int32.Parse(ViewState["RightIndex"].ToString());
            //如果注册的用户是校管理员
            if (k == 2)
            {
                collegeList.Visible = majorList.Visible = classList.Visible = false;
                Label6.Visible = Label7.Visible = Label8.Visible = false;
            }
            //如果是院管理员
            else if (k == 1)
            {
                 collegeList.Visible = Label6.Visible = true;
                majorList.Visible = classList.Visible = Label7.Visible = Label8.Visible = false;
            }
            else
            {
                collegeList.Visible = majorList.Visible = classList.Visible = true;
                Label6.Visible = Label7.Visible = Label8.Visible = true;
            }
        }

        protected void LinkButton_Announcement_Click(object sender, EventArgs e)
        {
            Response.Redirect("PublicAnnouncement.aspx");
        }
    
    }
}
